package jchat.genral;

import java.util.ArrayList;

import jchat.Controller.LocalStorageHandler;
import jchat.genral.MessageAdpter.ViewHolder.ImageViewHolder;
import jchat.genral.MessageAdpter.ViewHolder.TextViewHolder;
import jchat.services.IMService;
import jchat.types.ConversionType;
import jchat.types.MessageType;





import chat.jdaas.R;



import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

public class MessageAdpter extends BaseAdapter{
      private Context context;
	ArrayList<Users> list=new ArrayList<Users>();
	private final int VIEW_TYPE_TEXT 		= 0;
	private final int VIEW_TYPE_IMAGE 		= 1;
	MessageType type;
	
	ConversionType sendtype=new ConversionType();
	private LayoutInflater mInflater;
	public MessageAdpter(Context context, ArrayList<Users> objects) {
		// TODO Auto-generated constructor stub
		this.context=context;
		this.list=objects;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}
	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	@Override
     public int getItemViewType(int position) 
    {
		  if(list.get(position).getType().equals("text"))
	         
	        	 return VIEW_TYPE_TEXT;
	         
		  else if(list.get(position).getType().equals("image"))
			  return VIEW_TYPE_IMAGE;
		return VIEW_TYPE_TEXT;

   
    }
@Override
public int getViewTypeCount() {
    return 2;    //three different layouts to be inflated
}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Users user=(Users)this.getItem(position);
       ViewHolder.TextViewHolder textholder=null;
       LayoutParams lp,lpimage = null ;
       ViewHolder.ImageViewHolder imageholder=null;
		int type=getItemViewType(position);
		if(type==VIEW_TYPE_TEXT)
		{
				if(convertView==null)
				{
					if(user.getmine())
					{
						convertView=mInflater.inflate(R.layout.rightrowconversion, parent,false);
					}
					else
					{
						convertView=mInflater.inflate(R.layout.leftrowconversion, parent, false);
					}	
					textholder=new ViewHolder().new TextViewHolder();
					textholder.txt_msg=(TextView)convertView.findViewById(R.id.message);
				
					convertView.setTag(textholder);
					imageholder=null;
					
				}
				else
				{
				    textholder=(TextViewHolder)convertView.getTag();
				    imageholder=null;
				}
				textholder.txt_msg.setText(user.getMessage());
				
			lp= (LayoutParams) textholder.txt_msg.getLayoutParams();
			 
			 	
		}
		else if(type==VIEW_TYPE_IMAGE)
		{
			if(convertView==null)
			{
				convertView=mInflater.inflate(R.layout.imageholder, parent,false);
				imageholder=new ViewHolder().new ImageViewHolder();
				imageholder.imageview=(ImageView)convertView.findViewById(R.id.chatimage);
				convertView.setTag(imageholder);
				textholder=null;
			}
			else
			{
				imageholder=(ImageViewHolder)convertView.getTag();
				textholder=null;
			}
			String path=user.getMessage();
		
			Bitmap bmp = BitmapFactory.decodeFile(user.getMessage());
			imageholder.imageview.setImageBitmap(bmp);
		  

		}
		
		return convertView;
	}
	
	
	
	
	
	
	class ViewHolder
	{

        class TextViewHolder
        {
        	TextView txt_msg;
        	ImageView msgsent_alert;
        }
		class ImageViewHolder
		{
			ImageView imageview;
		}
	}
	
   

}
